package com.ai.cltcore.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符串工具类22
 */
public final class StringUtil {
	/** Private Constructor **/
	private StringUtil(){
	}
	
	/**
	 * 判断字符串是否非null && 非空字符 
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isNotEmpty(String param) {
		return param != null && !"".equals(param.trim());
	}

	/**
	 * 判断字符串是否为null || 空字符串
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isEmpty(String param) {
		return param == null || "".equals(param.trim());
	}
	
	/**
	 * 判断是否为数字
	 * @param str
	 * @return True为数字
	 */
	public static boolean isNum(String str) {
		String regex = "^(-?\\d+)(\\.\\d+)?$";
		return matchRegex(str, regex);
	}
	private static boolean matchRegex(String value, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(value);
		return matcher.matches();
	}
	
	/**
	 * 两个字段取值是否变动-带非空验证
	 * @Description: 
	 * @param a 
	 * @param b
	 * @return
	 * @ReturnType boolean
	 * @author: Chengfang.Su
	 * @Created 2016年8月23日 上午10:44:01
	 */
	public static boolean isChanged(String a, String b){
		if(null == a && null == b){
			return false;
		}else if(null == a || null == b){
			return true;
		}else if(a.equals(b)){
			return false;
		}else{
			return true;
		}
	}
	public static boolean isNotNullAndEmpty(String param) {
		return param != null && !"".equals(param.trim())&& !"null".equals(param);
	}

	/**
	 * 判断字符串是否为null || 空字符串
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isNullAndEmpty(String param) {
		return param == null || "".equals(param.trim()) || "null".equals(param);
	}
	/**
	 * 替换传递参数里面的特殊字符，
	 * 
	 * @param param 需要处理的字符串
	 * @param specialChar  被替换的特殊字符 
	 * @return
	 */
	public static String replaceChar(String param,String specialChar) {
		String newStr = param;//替换过后的字符；
		if(!StringUtil.isEmpty(param)){
			String replaceStr = param.replace(specialChar, "','");
			newStr = new StringBuilder().append("'").append(replaceStr).append("'").toString();
		}
		return newStr;
	}
	
	/**
	 * 替换传递参数里面的","，
	 * 
	 * @param param 需要处理的字符串
	 * @return
	 */
	public static String replaceChar(String param) {
		return replaceChar(param,",");
	}
	
	/**
	 * 根据身份证号计算出生日期
	 * @param credNum
	 * @return
	 */
	public static String calcBirthDate(String credNum){
		String  birthDate;
		if(credNum.length() != 15 && credNum.length() != 18){
			return "";
		}else if(credNum.length() == 15){
			birthDate = "19" + credNum.substring(6,12);
		}else{
			birthDate = credNum.substring(6,14);
		}
		return birthDate;
	} 
	
	/**
	 * 根据身份证号计算年龄
	 * @param credNum
	 * @return 返回负数时身份证号码入参错误
	 */
	public static int calcAgeByCredNum(String credNum){
		String  birthDate = calcBirthDate(credNum);
		if(isNotEmpty(birthDate)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String nowDate = sdf.format(new Date());
			int preAge = Integer.parseInt(nowDate.substring(0,4)) - Integer.parseInt(birthDate.substring(0,4)); 
			if(Integer.parseInt(birthDate.substring(4,6)) > Integer.parseInt(nowDate.substring(4,6))){
				preAge-- ;//出生月份大于当前月份，年龄减一
			}
			
			if(Integer.parseInt(birthDate.substring(4,6)) == Integer.parseInt(nowDate.substring(4,6))
					&& Integer.parseInt(birthDate.substring(6)) > Integer.parseInt(nowDate.substring(6))){
				//月份相同，日期大于当前日期，年龄减一
				preAge--;
			}
			return preAge;
		}else{
			return -1;
		}
	}
}
